package com.liuzh.kaoyanspringboot.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.common.Constants;
import com.liuzh.kaoyanspringboot.entity.Comment;
import com.liuzh.kaoyanspringboot.entity.Message;
import com.liuzh.kaoyanspringboot.entity.User;
import com.liuzh.kaoyanspringboot.mapper.MessageMapper;
import com.liuzh.kaoyanspringboot.service.CommentService;
import com.liuzh.kaoyanspringboot.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuzh.kaoyanspringboot.service.UserService;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-22
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Resource
    private MessageMapper messageMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;

    /**
     * 计算新消息数量
     * @param userId
     * @return
     */
    @Override
    public Integer getNewsByUserId(Integer userId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recipient_id",userId);
        queryWrapper.eq("is_new",true);
        Long res = messageMapper.selectCount(queryWrapper);
        return Math.toIntExact(res);
    }

    /**
     * 保存一个新消息
     * @param senderId
     * @param recipientId
     * @param type
     * @param replyId
     * @param description
     * @return
     */
    @Override
    public Message createAndSave(Integer senderId,Integer recipientId, String type, Integer replyId, String description) {
        Message res = new Message();
        res.setType(type);
        res.setReplyId(replyId);
        res.setSenderId(senderId);
        res.setCreateTime(DateUtil.now());
        if (!description.equals("")) {
            res.setDescription(description);
        }
        res.setRecipientId(recipientId);
        save(res);
        return res;
    }

    @Override
    public void createComment(Comment comment) {
        String nickname = userService.getById(comment.getUserId()).getNickname();
        Comment pComment = commentService.findById(comment.getPid());
        createAndSave(TokenUtils.getCurrentUser().getId(), pComment.getUserId(), Constants.TYPE_COMMENT,pComment.getId(),nickname + "回复了" + comment.getContent());
    }

    @Override
    public Page<Message> findPage(Page<Message> page, String SNickname, String RNickname, String type) {
        return messageMapper.findPage(page, SNickname,RNickname, type);
    }

    @Override
    public List<Comment> findReplyComment() {
        // 1.从 message 里面查询回复我被回复的消息
        List<Integer> replyCommentIds = messageMapper.getReplyIdsByTypeAndUserId(Constants.TYPE_COMMENT, TokenUtils.getCurrentUser().getId());
        if (replyCommentIds.size() == 0) return null;
        // 2.找到所有回复我的消息
        List<Comment> comments = commentService.fListByIds(replyCommentIds);
        for (Comment comment : comments) {
            comment.setChildren(commentService.findByPid(comment.getId()));
        }
        return comments;
    }

    @Override
    public List<Message> findByIsNew(boolean isNew, String SNickname, String RNickname, String type) {
        return messageMapper.findByIsNew(isNew, SNickname, RNickname, type);
    }

    @Override
    public boolean oldBatchByIds(List<Integer> ids) {
        return messageMapper.oldBatchByIds(ids);
    }

    @Override
    public List<User> concernMe(Integer userId) {
        return messageMapper.findConcernMe(userId);
    }

}
